package com.ruoyi.points.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.I18nException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.points.domain.VideoPointsLogs;
import com.ruoyi.points.service.IVideoPointsLogsService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.points.mapper.VideoPointsMapper;
import com.ruoyi.points.domain.VideoPoints;
import com.ruoyi.points.service.IVideoPointsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * pointsService业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@Service
public class VideoPointsServiceImpl implements IVideoPointsService 
{
    @Autowired
    private VideoPointsMapper videoPointsMapper;

    @Autowired
    private IVideoPointsLogsService logsService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询points
     * 
     * @param userId points主键
     * @return points
     */
    @Override
    public VideoPoints selectVideoPointsByUserId(Long userId)
    {
        VideoPoints videoPoints = videoPointsMapper.selectVideoPointsByUserId(userId);
        if(StringUtils.isNull(videoPoints)) {
            SysUser user = userService.selectUserById(userId);
            videoPoints = new VideoPoints();
            videoPoints.setPoints(0l);
            if (StringUtils.isNotNull(user)) {
                videoPoints.setUserId(userId);
                insertVideoPoints(videoPoints);
            }
        }
        return videoPoints;
    }

    public VideoPoints selectVideoPointsByUserName(String userName) {
        VideoPoints videoPoints = videoPointsMapper.selectVideoPointsByUserName(userName);
        if(StringUtils.isNull(videoPoints)) {
            videoPoints = new VideoPoints();
            videoPoints.setPoints(0l);
        }
        return videoPoints;
    }

    /**
     * 查询points列表
     * 
     * @param videoPoints points
     * @return points
     */
    @Override
    public List<VideoPoints> selectVideoPointsList(VideoPoints videoPoints)
    {
        return videoPointsMapper.selectVideoPointsList(videoPoints);
    }

    /**
     * 新增points
     * 
     * @param videoPoints points
     * @return 结果
     */
    @Override
    public int insertVideoPoints(VideoPoints videoPoints)
    {

        return videoPointsMapper.insertVideoPoints(videoPoints);
    }

    @Override
    public int addPoint(VideoPoints videoPoints) {
        return addPoint(videoPoints, "增加积分");
    }

    @Override
    public int addPoint(VideoPoints videoPoints, String message) {
        SysUser user = userService.selectUserById(videoPoints.getUserId());
        if (StringUtils.isNull(user)) {
            throw new I18nException("user.not.exists2");
        }
        VideoPoints videoPoints2 = videoPointsMapper.selectVideoPointsByUserId(videoPoints.getUserId());
        int i =0;
        if(StringUtils.isNull(videoPoints2)) {
            i = insertVideoPoints(videoPoints);
        } else {
            videoPoints2.addPoints(videoPoints.getPoints());
            i = updateVideoPoints(videoPoints2);
        }

        if (i > 0) {
            logsService.addVideoPointsLogs(videoPoints.getUserId(), videoPoints.getPoints(), message);
        }

        return i;
    }

    /**
     * 修改points
     * 
     * @param videoPoints points
     * @return 结果
     */
    @Override
    public int updateVideoPoints(VideoPoints videoPoints)
    {
        return videoPointsMapper.updateVideoPoints(videoPoints);
    }

    /**
     * 批量删除points
     * 
     * @param userIds 需要删除的points主键
     * @return 结果
     */
    @Override
    public int deleteVideoPointsByUserIds(Long[] userIds)
    {
        return videoPointsMapper.deleteVideoPointsByUserIds(userIds);
    }

    /**
     * 删除points信息
     * 
     * @param userId points主键
     * @return 结果
     */
    @Override
    public int deleteVideoPointsByUserId(Long userId)
    {
        return videoPointsMapper.deleteVideoPointsByUserId(userId);
    }

    /**
     * 积分变更
     * @param userId
     * @param points
     * @param notes
     * @return
     */
    @Override
    @Transactional
    public int changPoint(Long userId, Long points, String notes) {
        if (points == 0) return 1;
        VideoPoints videoPoints = selectVideoPointsByUserId(userId);
        if (StringUtils.isNull(videoPoints)) {
            videoPoints = new VideoPoints();
            videoPoints.setUserId(userId);
            videoPoints.setPoints(points);
            videoPointsMapper.insertVideoPoints(videoPoints);
        } else {
            videoPoints.setPoints(videoPoints.getPoints() + points);
            videoPointsMapper.updateVideoPoints(videoPoints);
        }

        VideoPointsLogs logs = new VideoPointsLogs();
        logs.setPoints(points);
        logs.setNotes(notes);
        logs.setUserId(userId);
        logsService.insertVideoPointsLogs(logs);

        return 1;
    }

    @Override
    @Transactional
    public int changPoint(VideoPoints videoPoints, Long points, String notes) {
        if (points == 0) return 1;
        videoPoints.setPoints(videoPoints.getPoints() + points);
        videoPointsMapper.updateVideoPoints(videoPoints);

        VideoPointsLogs logs = new VideoPointsLogs();
        logs.setPoints(points);
        logs.setNotes(notes);
        logs.setUserId(videoPoints.getUserId());
        logsService.insertVideoPointsLogs(logs);

        return 1;
    }
}
